package cn.ywang.spring.framework.aop.framework.adapter;

import cn.ywang.spring.framework.aop.aspect.AbstractAspectAdvice;
import cn.ywang.spring.framework.aop.aspect.Advice;
import cn.ywang.spring.framework.aop.intercept.Joinpoint;
import cn.ywang.spring.framework.aop.intercept.MethodInterceptor;
import cn.ywang.spring.framework.aop.intercept.MethodInvocation;

import java.lang.reflect.Method;

/**
 * @author BeatificWang
 * @date 2019/5/4 22:33
 */
public class MethodBeforeAdviceInterceptor extends AbstractAspectAdvice implements Advice, MethodInterceptor {

	private Joinpoint joinpoint;

	public MethodBeforeAdviceInterceptor(Method aspect, Object target) {
		super(aspect, target);
	}

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		//从被织入的代码中拿到Joinpoint
		this.joinpoint = mi;
		this.before(mi.getMethod(), mi.getArguments(), mi.getThis());
		return mi.proceed();
	}

	/**
	 * 待织入的方法
	 *
	 * @param method
	 * @param args
	 * @param target
	 * @throws Throwable
	 */
	private void before(Method method, Object[] args, Object target) throws Throwable {
		super.invokeAdviceMethod(this.joinpoint, null, null);
	}
}
